import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class Demo {
    public static void scan(File file, String tmp) throws IOException {
        File[]arr=file.listFiles();
        //如果当前文件下没有东西就返回
        if(arr==null){
            return;
        }
        //遍历数组
        for (File file1:arr) {
            //如果是目录就递归
            if(file1.isDirectory()){
                scan(file1,tmp);
            }else{
                //检查当前文件
                checkFile(file1,tmp);
            }
        }

    }

    public static void checkFile(File file,String tmp) throws IOException {
        //如果文件名包含则打印
        if(file.getName().contains(tmp)){
            System.out.println(file.getCanonicalPath()+" 包含: "+tmp);
        }
        //判断文件内容是否包含
        try(InputStream inputStream=new FileInputStream(file)){
            StringBuilder stringBuilder=new StringBuilder();
            Scanner scanner=new Scanner(inputStream);
            //按行读取 遇到\n就重新判断内容是否含tmp
            while(scanner.hasNextLine()){
                stringBuilder.append(scanner.nextLine()+"\n");
            }
            if(stringBuilder.indexOf(tmp)>-1){
                System.out.println(file.getCanonicalPath()+" 包含: "+tmp);
            }
        };

    }
}
